package com.yiyuan.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.yiyuan.common.base.BaseDao;
import com.yiyuan.system.dao.IRoleDao;
import com.yiyuan.system.model.Role;

@Repository("roleDao")
public class RoleDaoHbImpl extends BaseDao implements IRoleDao {
	public void add(Role role) {
		this.getHibernateTemplate().save(role);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findList(int startIndex, int endIndex,String roleName) {
		Session session = this.getSession();
		String hql="from Role ";
		if(roleName!=null && !roleName.equals("")){
			hql+=" where roleName like '%'||:roleName||'%' ";
		}
		Query query = session.createQuery(hql);
		if(roleName!=null && !roleName.equals("")){
			query.setString("roleName", roleName);
		}
		query.setFirstResult(startIndex - 1);
		query.setMaxResults(endIndex);
		List<Role> list = query.list();
		return list;
	}

	public int getPageCount(String roleName) {
		Session session = this.getSession();
		String hql="select count(*) from Role ";
		if(roleName!=null && !roleName.equals("")){
			hql+=" where roleName like '%'||:roleName||'%' ";
		}
		Query query = session.createQuery(hql);
		if(roleName!=null && !roleName.equals("")){
			query.setString("roleName", roleName);
		}
		int count = Integer.parseInt(query.uniqueResult().toString());
		return count;
	}

	public void update(Role role) {
		this.getHibernateTemplate().update(role);
	}

	public Role get(Integer roleId) {
		return (Role) this.getHibernateTemplate().get(Role.class, roleId);
	}

	public void delete(Role role) {
		this.getHibernateTemplate().delete(role);
	}

	@SuppressWarnings("unchecked")
	public List<Role> findList() {
		return this.getHibernateTemplate().find("from Role");
	}
}
